package com.wlyuan.open.controller;

import com.wlyuan.open.api.IOpenApiService;
import com.wlyuan.open.dto.api.*;
import com.wlyuan.open.vo.OpenApiDetailVO;
import com.wlyuan.utils.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author lijing
 * @description
 */
@Slf4j
@Api(tags = "openApi管理")
@Validated
@RequestMapping("")
@RestController
public class OpenApiController {
    @DubboReference(check = false)
    private IOpenApiService openApiService;

    @ApiOperation(value = "创建api")
    @PostMapping("")
    public void create(@Valid @RequestBody OpenApiCreationDTO creationDTO) {
        openApiService.create(creationDTO);
    }

    @ApiOperation(value = "查询所有api")
    @PostMapping("/search")
    public Page<OpenApiDetailVO> search(@RequestBody OpenApiFilterDTO filter) {
        return openApiService.search(filter);
    }

    @ApiOperation(value = "根据id获取api详情")
    @GetMapping("/{apiId}")
    public OpenApiDetailVO getByApiId(@PathVariable("apiId") Long apiId) {
        return openApiService.getApiId(apiId);
    }

    @ApiOperation(value = "启用")
    @PutMapping("/enable")
    public void enable(@Valid @RequestBody OpenApiEnableDTO enable) {
        openApiService.enable(enable);
    }

    @ApiOperation(value = "禁用")
    @PutMapping("/disable")
    public void disable(@Valid @RequestBody OpenApiDisableDTO disable) {
        openApiService.disable(disable);
    }

    @ApiOperation(value = "修改api")
    @PutMapping("/{apiId}")
    public void update(@PathVariable("apiId") Long apiId, @Valid @RequestBody OpenApiUpdateDTO updateDTO) {
        updateDTO.setApiId(apiId);
        openApiService.update(updateDTO);
    }
}
